package com.haidechizi.sort;

import java.util.Arrays;

/**
 * @author: wangdejun
 * @create: 2020-04-14 10:12
 * 时间复杂度：O(n^2)
 * 空间复杂度：O(1)
 **/
public class ShellSort {

    public static void main(String[] args) {
        int[] array = new int[]{9, 3, 17, 8, 5, 6, 7, 3, 5, 6, 2, 1, 33};
        shellSort(array);
        System.out.println(Arrays.toString(array));

    }

    public static void shellSort(int[] array) {
        int length = array.length;
        for (int d = length / 2; d > 0; d = d / 2) {
            for (int i = d; i < length; i++) {
                int temp = array[i];
                int j;
                for (j = i - d; j >= 0 && array[j] > temp; j = j - d) {
                    array[j + d] = array[j];
                }
                array[j + d] = temp;
            }
        }
    }
}
